package com.guowen.myaccount;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.guowen.myaccount.db.DBManager;
import com.guowen.myaccount.db.MenuBean;
import com.guowen.myaccount.utils.MenuDialog;

import java.util.ArrayList;
import java.util.List;

public class PersonActivity extends AppCompatActivity implements View.OnClickListener{

    private MyPersonAdapter adapter;
    private ListView personLv;
    private ImageView menuIv;
    private List<MenuBean> mDatas1, mDatas2;
    private TextView recordTv, recordNumTv, sortTv, sortNumTv, accountTv, accountNumTv;
    private Context context = this;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person);
        menuIv = findViewById(R.id.activity_person_title_iv);
        personLv = findViewById(R.id.activity_person_lv);
        recordTv = findViewById(R.id.activity_person_tv_record);
        recordNumTv = findViewById(R.id.activity_person_tv_record_num);
        sortTv = findViewById(R.id.activity_person_tv_sort);
        sortNumTv = findViewById(R.id.activity_person_tv_sort_num);
        accountTv = findViewById(R.id.activity_person_tv_account);
        accountNumTv = findViewById(R.id.activity_person_tv_account_num);

        int recordNum = DBManager.getAccountList(0).size() + DBManager.getAccountList(1).size();
        recordNumTv.setText(recordNum+"");
        int sortNum = DBManager.getTypeList(0).size() + DBManager.getTypeList(1).size();
        sortNumTv.setText(sortNum+"");
        int accountNum = DBManager.getAccManList().size();
        accountNumTv.setText(accountNum+"");

        // 初始化菜单数据源
        mDatas1 = DBManager.getMenuBean(0);
        mDatas2 = DBManager.getMenuBean(1);

        adapter = new MyPersonAdapter();
        personLv.setAdapter(adapter);

        menuIv.setOnClickListener(this);
        recordTv.setOnClickListener(this);
        recordNumTv.setOnClickListener(this);
        sortTv.setOnClickListener(this);
        sortNumTv.setOnClickListener(this);
        accountTv.setOnClickListener(this);
        accountNumTv.setOnClickListener(this);

        personLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = null;
                switch (position) {
                    case 10:
                        break;
                    default:
                        Toast.makeText(context,"此功能还未开发，敬请期待！",Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.activity_person_title_iv:
                MenuDialog menuDialog = new MenuDialog(this, mDatas1, mDatas2);
                menuDialog.show();
                menuDialog.setDialogSize();
                break;
            case R.id.activity_person_tv_record:
            case R.id.activity_person_tv_record_num:
                intent = new Intent(this, HistoryActivity.class);
                startActivity(intent);
                break;
            case R.id.activity_person_tv_sort:
            case R.id.activity_person_tv_sort_num:
                intent = new Intent(this,SortActivity.class);
                startActivity(intent);
                break;
            case R.id.activity_person_tv_account:
            case R.id.activity_person_tv_account_num:
                intent = new Intent(this,AccManagementActivity.class);
                startActivity(intent);
                break;
        }

    }

    class MyPersonAdapter extends BaseAdapter{

        List<PersonBean> list;

        public MyPersonAdapter(){
            list = new ArrayList<>();
            float sum = DBManager.getSumMoneyAll();
            list.add(new PersonBean(R.mipmap.budget,String.valueOf(sum),"预算管理"));
            list.add(new PersonBean(R.mipmap.time,"每月1日","帐本周期"));
            list.add(new PersonBean(R.mipmap.output,"","导入导出"));
            list.add(new PersonBean(R.mipmap.lookup,"","账目搜索"));
            list.add(new PersonBean(R.mipmap.perbackup,"","备份历史"));
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate(R.layout.item_person_lv,parent,false);
            ViewHolder holder = new ViewHolder(convertView);
            PersonBean bean = list.get(position);
            holder.itemIv.setImageResource(bean.getImageId());
            holder.beizhuTv.setText(bean.getBeizhu());
            holder.typeTv.setText(bean.getTypename());
            return convertView;
        }
    }

    class ViewHolder{
        private ImageView itemIv;
        private TextView typeTv, beizhuTv;

        public ViewHolder(View view){
            itemIv = view.findViewById(R.id.item_person_lv_iv);
            typeTv = view.findViewById(R.id.item_person_lv_tv);
            beizhuTv = view.findViewById(R.id.item_person_lv_tv_beizhu);
        }
    }

    class PersonBean{
        private int imageId;
        private String beizhu, typename;

        public PersonBean(int imageId, String beizhu, String typename) {
            this.imageId = imageId;
            this.beizhu = beizhu;
            this.typename = typename;
        }

        public PersonBean() {
        }

        public int getImageId() {
            return imageId;
        }

        public void setImageId(int imageId) {
            this.imageId = imageId;
        }

        public String getBeizhu() {
            return beizhu;
        }

        public void setBeizhu(String beizhu) {
            this.beizhu = beizhu;
        }

        public String getTypename() {
            return typename;
        }

        public void setTypename(String typename) {
            this.typename = typename;
        }
    }
}